﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ICC
    {
        public DataSet CC_GetList(out int ItemCount, int CandidateID, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCertificate_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CandidateID", CandidateID);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public CCInfo CC_GetInfo(int _CC_ID)
        {
            CCInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCertificate_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CC_ID", _CC_ID);
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CCInfo();
                    retVal.ID = Convert.ToInt32(dr["CC_ID"]);
                    retVal.CandidateID = Convert.ToInt32(dr["CC_CandidateID"]);
                    retVal.Name = Convert.ToString(dr["CC_Name"]);
                    retVal.AuthorityIssue = Convert.ToString(dr["CC_AuthorityIssue"]);
                    retVal.IssueDate = dr["CC_IssueDate"]==DBNull.Value? DateTime.MinValue: Convert.ToDateTime(dr["CC_IssueDate"]);
                    retVal.IssueDate = dr["CC_FromDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["CC_FromDate"]);
                    retVal.IssueDate = dr["CC_ToDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["CC_ToDate"]);
                    retVal.Grade = Convert.ToString(dr["CC_Grade"]);
                    retVal.Speciality = Convert.ToString(dr["CC_Speciality"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int CC_Insert(CCInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCertificate_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@CC_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CC_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@CC_AuthorityIssue", eInfo.AuthorityIssue);
            if(eInfo.IssueDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CC_IssueDate", eInfo.IssueDate);
            else
                dbCmd.Parameters.AddWithValue("@CC_IssueDate", DBNull.Value);
            if (eInfo.FromDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CC_FromDate", eInfo.FromDate);
            else
                dbCmd.Parameters.AddWithValue("@CC_FromDate", DBNull.Value);
            if (eInfo.ToDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CC_ToDate", eInfo.ToDate);
            else
                dbCmd.Parameters.AddWithValue("@CC_ToDate", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@CC_Grade", eInfo.Grade);
            dbCmd.Parameters.AddWithValue("@CC_Speciality", eInfo.Speciality);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CC_Update(CCInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCertificate_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CC_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@CC_CandidateID", eInfo.CandidateID);
            dbCmd.Parameters.AddWithValue("@CC_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@CC_AuthorityIssue", eInfo.AuthorityIssue);
            if (eInfo.IssueDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CC_IssueDate", eInfo.IssueDate);
            else
                dbCmd.Parameters.AddWithValue("@CC_IssueDate", DBNull.Value);
            if (eInfo.FromDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CC_FromDate", eInfo.FromDate);
            else
                dbCmd.Parameters.AddWithValue("@CC_FromDate", DBNull.Value);
            if (eInfo.ToDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CC_ToDate", eInfo.ToDate);
            else
                dbCmd.Parameters.AddWithValue("@CC_ToDate", DBNull.Value);
            dbCmd.Parameters.AddWithValue("@CC_Grade", eInfo.Grade);
            dbCmd.Parameters.AddWithValue("@CC_Speciality", eInfo.Speciality);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CC_Delete(int CCID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCertificate_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CC_ID", CCID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool CC_Exist(int _ID, int _CandidateID, string _AuthorityIssue)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_CandidateCertificate_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CC_ID", _ID);
            dbCmd.Parameters.AddWithValue("@CC_CandidateID", _CandidateID);
            dbCmd.Parameters.AddWithValue("@CC_AuthorityIssue", _AuthorityIssue);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

